/*---------------------------------------------------------
   Needed Information
---------------------------------------------------------*/

DeriveGamemode("sandbox") -- Deriving from sandbox, this allows to spawn props, vehicles, use tools etc.
AddCSLuaFile("cl_init.lua") -- If you can't understand these then go read the Wiki.
AddCSLuaFile("shared.lua")
AddCSLuaFile("specialchars.lua")
include("shared.lua") -- Getting the clients to download these files.
include("specialchars.lua") -- the 'include' should only have shared files or clientside files.
include("cl_init.lua")

function GM:PlayerSpawn( ply )
 
    self.BaseClass:PlayerSpawn( ply )   // Lines 17 through 21 are all fixes to the sandbox glitch. Don't change
										// them unless you know what you're doing.
    ply:SetGravity( 0.75 )  
    ply:SetMaxHealth( 100, true )  
 
    ply:SetWalkSpeed( 400 )  
	ply:SetRunSpeed( 300 ) 
 
end

/*-----------------------------------------------------------------------------------
   Checking special characters, if you deleted file delete this and lines 29 to 37
-----------------------------------------------------------------------------------*/
 
function GM:PlayerInitialSpawn( ply )
	CheckSpecialCharacters( ply )
	if ply:IsAdmin() then -- if the player is an admin
		sb_team2( ply ) -- then put him on team 2 (admin)
	else
	sb_team3( ply )
	RunConsoleCommand( "sb_start" ) 
	end
end

/*---------------------------------------------------------
   Nocollide People: Default is ON
---------------------------------------------------------*/

function NoCollidePeople( teamID )
	for _, ply in pairs( team.GetPlayers( teamID ) ) do -- Getting the team players, then
		ply:SetNoCollideWithTeammates( true ) -- Setting nocollide with each other to TRUE
	end -- Ending the 'for'
end -- Ending the function
 
/*---------------------------------------------------------
   Weapon and Other setup for teams.
---------------------------------------------------------*/

function GM:PlayerLoadout( ply )
 
	if ply:Team() == 1 then
 
		ply:SetModel("models/player/zombiefast.mdl") -- This is the builders player model.
		ply:SetHealth(100) -- They're health
		ply:SetRunSpeed(250) -- When pressing shift, this is they're speed
		ply:SetWalkSpeed(200) -- When just walking, this is they're speed
		ply:SetJumpPower(250) -- Jump power?
		ply:StripWeapons() -- Strip they're current weapons.
		ply:Give("weapon_physcannon") -- Give them these weapons
		ply:Give("weapon_physgun")
		ply:Give("gmod_tool")
		ply:Give("weapon_crowbar")
 
 
	elseif ply:Team() == 2 then
 
		ply:SetModel("models/player/swat.mdl") -- Admin's Model
		ply:SetHealth(150) -- Health
		ply:SetRunSpeed(275) -- Pressing shift, this is they're speed
		ply:SetWalkSpeed(225) -- Walking, this is they're speed
		ply:SetJumpPower(300) -- Jump power?
		ply:StripWeapons() -- Strip they're current weapons.
		ply:Give("weapon_physcannon") -- Give them these weapons
		ply:Give("weapon_physgun")
		ply:Give("weapon_pistol")
		ply:Give("gmod_tool")
		ply:Give("gmod_camera")
		ply:Give("weapon_crowbar")
		
	
	elseif ply:Team() == 3 then
		
		ply:Spectate() -- Make them spectator.
		ply:StripWeapons() -- Strip they're weapons since they're dead.
	end -- end the if and elseif's
end -- End the functions
 
function sb_team1( ply )
 
	ply:UnSpectate() -- Unspectate them so they can play.
	ply:SetTeam( TEAM_BUILDER ) -- We set his team to Builder
	ply:Spawn() -- Spawn the player
	ply:PrintMessage( HUD_PRINTTALK, "[CBuild]Welcome to the server, " .. ply:Nick() ) -- Print the welcoming message.
 
end -- Ending the function
 
 
function sb_team2( ply )
	-- Why no unspectate? Look carefully at the GM:PlayerSpawn; We only call to spectate after we see if he's an admin. Assuming he's always ready to build, I chose to skip it.	
	ply:SetTeam(2)
	ply:Spawn()
	ply:PrintMessage( HUD_PRINTTALK, "[CBuild]Welcome back admin, or should I say " .. ply:Nick() ) 

 
end
concommand.Add( "sb_team1", sb_team1 )
 
function joining( ply )
 
	ply:Spectate(4)
	ply:SetTeam(3)
 
end

/*--------------------------------------
   Map Rotation System
--------------------------------------*/

local mRotateTimeLeft=7200 -- This is the round time in seconds (3600= 1 hr)
mapsList={"gm_construct","gm_flatgrass"} -- ie. mapsList={"gm_construct","gm_flatgrass"}
timeWarnings={}
timeWarnings[3600]="1 HOUR BEFORE NEXT MAP." --  this is the time warnings
timeWarnings[1800]="30 MINUTES BEFORE NEXT MAP!"
timeWarnings[900]="Better start packing up, 15 minutes until next map!!"
timeWarnings[31]="30 Seconds left, Save your stuff now!"
timeWarnings[6]="5"
timeWarnings[5]="4"
timeWarnings[4]="3"
timeWarnings[3]="2"
timeWarnings[2]="1"
timeWarnings[1]="CHANGING MAP"

if(SERVER) then
function nextMap()
	cMapName=string.lower(game.GetMap())
	cMapID=1
	nMap="gm_construct"

	for k,v in pairs(mapsList) do
		if(string.lower(v)==cMapName) then
			cMapID=k
			if(k==table.Count(mapsList)) then
				nMap=mapsList[1]
			else
				nMap=mapsList[k+1]
			end
		end
	end
	
	return nMap
end
function MapRotate()
	local nMap=nextMap()
	game.ConsoleCommand("changelevel " .. nMap .. "\n")
end

function setcountdown()
	if(GetGlobalInt("MapRotateCountDown")<1) then
		MapRotate()
		return
	end
	local cTime=GetGlobalInt("MapRotateCountDown")-1
	SetGlobalInt("MapRotateCountDown",cTime)
	SetGlobalString("MapRotateCountDownFormat", SecondsToFormat(cTime))
	if(timeWarnings[cTime]!=null) then
		for k,v in pairs(player.GetAll()) do
			v:PrintMessage(HUD_PRINTCENTER, timeWarnings[cTime])
		end
	end
	--- START last 10 seconds left to freeze and show the scoreboard to all. 
	if(cTime==10) then
      for _,pl in pairs(player.GetAll()) do
         pl:Freeze(true)
         pl:ConCommand("+showscores")
      end
	end
	--- END last 10 seconds left to freeze and show the scoreboard to all. 
end

function SecondsToFormat( secs )
	local hours=math.floor(secs/60/60)
	local mins=math.floor(secs/60)
	local seconds=0
	local timeleft=""
	if not(hours==0) then
		timeleft=hours .. ":"
	end
	if not(mins==0) then
		timeleft=timeleft .. mins .. ":"
	else
		timeleft=timeleft .. "00:"
	end
	seconds=secs - (hours*3600) - (mins*60)
	if (seconds<10) then
		seconds="0" .. seconds
	end
	timeleft=timeleft .. seconds
	
	return timeleft
end

timeWarnings[7]="Switching to " .. nextMap() .. " in..."
SetGlobalInt("MapRotateCountDown",mRotateTimeLeft)
SetGlobalString("MapRotateCountDownFormat", SecondsToFormat(mRotateTimeLeft))
timer.Create("MapRotateCountDown", 1, 0, setcountdown)
end